package com.yzy.soft.system.controller;

import com.yzy.soft.core.util.ImageUtil;
import com.yzy.soft.system.service.IGooflowService;
import com.yzy.soft.common.persistence.model.Gooflow;
import com.yzy.soft.common.persistence.dao.GooflowMapper;
import com.yzy.soft.system.dto.TableDataDto;
import com.yzy.soft.core.base.controller.BaseController;

import com.yzy.soft.core.base.tips.Tip;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.apache.ibatis.session.RowBounds;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.List;

/**
* 描述：前端流程图控制控制层
* @author Yinzhiyuan
* @date 2018-03-26 01:04:08
*/
@Api(value = "前端流程图控制Controller",description = "前端流程图控制控制接口")
@RestController
@RequestMapping("/gooflow")
public class GooflowController extends BaseController{

    private static final Logger logger= LoggerFactory.getLogger(GooflowController.class);

    private static final String PATH=ClassUtils.getDefaultClassLoader().getResource("").getPath();
    @Autowired
    private IGooflowService gooflowService;
    
    @Autowired
    private GooflowMapper gooflowMapper;

    
    
    /**
    * 描述：查询 前端流程图控制列表
    */
    @ApiOperation("查询前端流程图控制")
    @ApiImplicitParams({@ApiImplicitParam(name="page",value="当前页数",dataType="int", paramType = "query"),@ApiImplicitParam(name="limit",value="页数据条数",dataType="int", paramType = "query")})
    @GetMapping(value = "",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public TableDataDto getAll(@RequestParam(name = "page", defaultValue = "1", required = true) int page, @RequestParam(name = "limit", defaultValue = "10", required = true) int pageSize)throws Exception {
    
        Wrapper<Gooflow> gooflow=new EntityWrapper<Gooflow>();
        page=(page-1)*pageSize;
        pageSize=page+pageSize;
        List<Gooflow> result=gooflowMapper.selectPage(new RowBounds(page,pageSize),gooflow);
        TableDataDto tableDataDto=new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(gooflowMapper.selectCount(gooflow));
        tableDataDto.setData(result);
        return tableDataDto;
    }

    /**
     * 描述：根据前端流程图控制编号查询前端流程图控制信息
     */
    @ApiOperation("根据前端流程图控制编号查询前端流程图控制信息")
    @GetMapping(value = "/{id}")//,produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    public Gooflow getById(@ApiParam(name="编码") @PathVariable("id") String id)throws Exception {
        return gooflowMapper.selectById(id);
    }

    

    /**
    * 描述:创建前端流程图控制
    * @param gooflow  前端流程图控制DTO
    */
    @ApiOperation("创建前端流程图控制")
    @PostMapping("")
    public Tip create(@ModelAttribute Gooflow gooflow) throws FileNotFoundException{
        String path=PATH+"static/pic/"+System.currentTimeMillis()+".png";
        ImageUtil.base64StringToImage(gooflow.getBase64().substring(22),path);
        String thumbnail=String.valueOf(System.currentTimeMillis());
        String thumbnailPath=PATH+"static/thumbnail/"+thumbnail+".png";
        gooflow.setThumbnail(ImageUtil.thumbnail(new FileInputStream(new File(path)),thumbnailPath,200,200));
        gooflow.setThumbnailname("/thumbnail/"+thumbnail+".png");
        gooflow.setPic(path);
        gooflow.setCreate(new Date());
        gooflow.setUpdate(new Date());
        Integer result=gooflowMapper.insert(gooflow);
        return SUCCESS_TIP;
    }

    /**
    * 描述：删除前端流程图控制
    * @param id 前端流程图控制id
    */
    @ApiOperation("删除前端流程图控制")
    @DeleteMapping(value = "",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Tip deleteById(@ModelAttribute Gooflow gooflow) throws Exception {
        Integer result=gooflowMapper.deleteById(gooflow.getId());
        return SUCCESS_TIP;
    }

    /**
    * 描述：更新前端流程图控制
    * @param id 前端流程图控制id
    */
    @ApiOperation("更新前端流程图控制")
    @PutMapping(value = "")//, produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    public Tip updateGooflow(@ModelAttribute Gooflow gooflow) throws Exception {
        String path=PATH+"static/pic/"+System.currentTimeMillis()+".png";
        ImageUtil.base64StringToImage(gooflow.getBase64().substring(22),path);
        String thumbnail=String.valueOf(System.currentTimeMillis());
        String thumbnailPath=PATH+"static/thumbnail/"+thumbnail+".png";
        gooflow.setThumbnail(ImageUtil.thumbnail(new FileInputStream(new File(path)),thumbnailPath,200,200));
        gooflow.setThumbnailname("/thumbnail/"+thumbnail+".png");
        gooflow.setPic(path);
        gooflow.setUpdate(new Date());
        Integer result=gooflowMapper.updateById(gooflow);
        return SUCCESS_TIP;
    }

}